/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.test.util;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.name.Named;

/**
 * Driver Manager, implements <code>DataSource</code> interface for getting connections to a database.
 * @author Adam
 * @version $Id: DriverManagerDataSource.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Sep 28, 2008 at 3:19:52 PM 
 */
public class DriverManagerDataSource implements DataSource {
	/**
	 * The class name for the JDBC Driver.
	 */
	@Named("config.jdbc.driverClassName") @Inject(optional = true)
	protected String driverClassName;

    /**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private static final Logger LOG = LoggerFactory.getLogger(DriverManagerDataSource.class);

	/**
	 * The database password for the given user. 
	 */
    @Named("config.jdbc.url.password") @Inject(optional = true)
	protected String password;

	/**
	 * A database url of the form <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>.
	 */
    @Named("config.jdbc.url") @Inject(optional = true)
	protected String url;

	/**
	 * The database username on whose behalf the connection is being made.
	 */
    @Named("config.jdbc.username") @Inject(optional = true)
	protected String username;

	/**
	 * Default Constructor.
	 */
	public DriverManagerDataSource() {
		super();
	}

	/**
	 * 
	 * @param driverClassName The class name for the JDBC Driver.
	 * @param url A database url of the form <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>.
	 */
	public DriverManagerDataSource(final String driverClassName, final String url) {
		this.driverClassName = driverClassName;
		this.url = url;
	}

	/**
	 * 
	 * @param driverClassName The class name for the JDBC Driver.
	 * @param url A database url of the form <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>.
	 * @param username The database username on whose behalf the connection is being made.
	 * @param password The database password for the given username. 
	 */
	public DriverManagerDataSource(final String driverClassName, final String url, final String username,
			final String password) {
		this.driverClassName = driverClassName;
		this.url = url;
		this.username = username;
		this.password = password;
	}

	/**
	 * {@inheritDoc}
	 * @see javax.sql.DataSource#getConnection()
	 */
	public Connection getConnection() throws SQLException {
		return getConnection(this.username, this.password);
	}

	/**
	 * {@inheritDoc}
	 * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
	 */
	public Connection getConnection(final String username, final String password) throws SQLException {
		try {
			Class.forName(driverClassName);
		} catch (ClassNotFoundException e) {
			LOG.error("ClassNotFoundException", e);
			throw new SQLException(e.getMessage());
		}
		if (username == null) {
			return DriverManager.getConnection(url);
		}
		return DriverManager.getConnection(url, username, password);
	}

	/**
	 * {@inheritDoc}
	 * @see javax.sql.CommonDataSource#getLoginTimeout()
	 */
	public int getLoginTimeout() throws SQLException {
		return DriverManager.getLoginTimeout();
	}

	/**
	 * {@inheritDoc}
	 * @see javax.sql.CommonDataSource#getLogWriter()
	 */
	public PrintWriter getLogWriter() throws SQLException {
		return DriverManager.getLogWriter();
	}

	/**
	 * {@inheritDoc}
	 * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
	 */
	public boolean isWrapperFor(final Class<?> iface) throws SQLException {
		return false;
	}

	/**
	 * {@inheritDoc}
	 * @see javax.sql.CommonDataSource#setLoginTimeout(int)
	 */
	public void setLoginTimeout(final int seconds) throws SQLException {
		DriverManager.setLoginTimeout(seconds);
	}

	/**
	 * {@inheritDoc}
	 * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
	 */
	public void setLogWriter(final PrintWriter out) throws SQLException {
		DriverManager.setLogWriter(out);
	}

	/**
	 * {@inheritDoc}
	 * @see java.sql.Wrapper#unwrap(java.lang.Class)
	 */
	public <T> T unwrap(final Class<T> iface) throws SQLException {
		return null;
	}

}
